﻿using System;
using System.Data;
using System.Collections.Generic;
using Besture.Common;
using Besture.Dal;
using Besture.Model;

namespace Besture.Bll
{
    public partial class OrderDac : BaseFacade
    {
        //private readonly OrderDal excuteDal = new OrderDal();

        /// <summary>
        /// 通过订单号 获取订单信息
        /// </summary>
        /// <param name="sOrderNo"></param>
        /// <returns></returns>
        public OrderModel GetModelByOrderNo(string sOrderNo)
        {
            try
            {
                base.CreateConnection();
                OrderDal excuteDal = new OrderDal();
                return excuteDal.GetModelByOrderNo(sOrderNo);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                CloseConnection();
            }
        }


        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <param name="model">数据模型</param>
        /// <returns></returns>
        public int Add(OrderModel model)
        {
            try
            {
                base.CreateConnection(); 
                base.BeginTransaction();
                OrderDal excuteDal = new OrderDal();
                int lsRecord = 0;
                lsRecord = excuteDal.Add(model);
                if (lsRecord > 0)
                {
                    base.Commit();
                    return lsRecord;
                }
                else
                {
                    base.RollBack();
                    return -1;
                }
            }
            catch (Exception e)
            {
                base.RollBack();
                throw e;
            }
            finally
            {
                base.CloseConnection();
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">数据模型</param>
        /// <returns></returns>
        public int Update(OrderModel model) 
        {
            try
            {
                base.CreateConnection();
                base.BeginTransaction();
                OrderDal excuteDal = new OrderDal();
                int lsRecord = 0;
                lsRecord = excuteDal.Update(model);
                if (lsRecord > 0)
                {
                    base.Commit();
                    return lsRecord;
                }
                else
                {
                    base.RollBack();
                    return -1;
                }
            }
            catch (Exception e)
            {
                base.RollBack();
                throw e;
            }
            finally
            {
                base.CloseConnection();
            }
        }

           public int UpdateByModel(OrderModel model)
        {
            try
            {
                base.CreateConnection();
                base.BeginTransaction();
                OrderDal excuteDal = new OrderDal();
                int lsRecord = 0;
                lsRecord = excuteDal.UpdateByModel(model);
                if (lsRecord > 0)
                {
                    base.Commit();
                    return lsRecord;
                }
                else
                {
                    base.RollBack();
                    return -1;
                }
            }
            catch (Exception e)
            {
                base.RollBack();
                throw e;
            }
            finally
            {
                base.CloseConnection();
            }
        }
        
        /// <summary>
        /// 获取数据模型
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
       public OrderModel GetModel(int id)
        {
            try
            {
                base.CreateConnection();
                OrderDal excuteDal = new OrderDal();
                return excuteDal.GetModel(id);
            }
            catch (Exception e)
            {
                throw e;
            }
           finally
            {
                 CloseConnection();
            }
        }

        /// <summary>
       /// 获取全部数据
       /// </summary>
       /// <returns></returns>
       public DataTable GetDataTable()
        {
            try
            {
                base.CreateConnection();
                OrderDal excuteDal = new OrderDal();
                return excuteDal.GetDataTable();
            }
            catch (Exception e)
            {
                throw e;
            }
           finally
            {
               CloseConnection();
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public int Delete(int id)
        {
            try
            {
                base.CreateConnection();
                OrderDal excuteDal = new OrderDal();
                return excuteDal.Delete(id);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
               CloseConnection();
            }
        }

        
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idList">主键值集合</param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            try
            {
                base.CreateConnection();
                OrderDal excuteDal = new OrderDal();
                return  excuteDal.DeleteList(ids);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
              CloseConnection();
            }
        }

        /// <summary>
        /// 查询订单页面上的基本信息
        /// </summary>
        /// <param name="hotelid"></param>
        /// <param name="roomid"></param>
        /// <param name="bt"></param>
        /// <param name="et"></param>
        /// <returns></returns>
        public DataSet GetHotelRoomInfo(int usertype, int hotelid, int roomid, string bt, string et)
        {
            try
            {
                base.CreateConnection();
                OrderDal excuteDal = new OrderDal();
                return excuteDal.GetHotelRoomInfo(usertype, hotelid, roomid, bt, et);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                CloseConnection();
            }
        }

        /// <summary>
        /// 添加订单，还未付款
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddNoPayOrder(OrderInfo_Order_Model model, int userid)
        {
            try
            {
                base.CreateConnection();
                OrderDal excuteDal = new OrderDal();
                return excuteDal.AddNoPayOrder(model, userid);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                CloseConnection();
            }
        }

        /// <summary>
        /// 查询订单，还未付款
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OrderPay_Order_Model GetNoPayOrder(int orderid, int userid)
        {
            try
            {
                base.CreateConnection();
                OrderDal excuteDal = new OrderDal();
                return excuteDal.GetNoPayOrder(orderid, userid);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                CloseConnection();
            }
        }

        /// <summary>
        /// 根据订单号，更新订单表
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public int UpdateOrder(string orderNo, int consumeIntegration, decimal deductiblePrice, decimal nightNumberPrice, decimal actuallyPaidPrice, int payMethod, int userID)
        {
            try
            {
                base.CreateConnection();
                OrderDal excuteDal = new OrderDal();
                return excuteDal.UpdateOrder(orderNo, consumeIntegration, deductiblePrice, nightNumberPrice, actuallyPaidPrice, payMethod, userID);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                CloseConnection();
            }
        }
    }
}
